<?php

/*
  ###############################################
  ####                                       ####
  ####    Author : Harish Chauhan            ####
  ####    Date   : 31 Dec,2004               ####
  ####    Updated:                           ####
  ####                                       ####
  ###############################################

 */

Class MysqlExcel {

    public $db;
    public $error = ""; //String

    function MysqlExcel() {
        
    }

    /*
     * @Params
     * 		$host : Host name of mysql database
     * 		$user : User
     * 		$pwd  : Password
     * 		$databse : database name
     */

    function openDatabase($host = null, $user = null, $pwd = null, $database = null) {
        if ($this->db == null) {
            $this->db = new DB();
            $this->db->open($host, $user, $pwd, $database);
            if (!$this->db) {
                $this->error = $this->db->error();
                return false;
            }
        }
    }

    /*
     * @Params 
     * 		$tnlName=Name of table from the data is imported to excel file
     * 		$fileName=Name of the file wants to save as.On leaving balnk it takes the table name as file name
     * 		$download=Boolean value whether you wants to save on disk or you want to force to download the file.
     */

    function ImportDataFromTable($tblName, $fileName = "", $download = false) {
        if (empty($fileName))
            $fileName = $tblName . ".xls";
        $sql = "SELECT * FROM $tblName";
        return $this->ImportData($sql, $fileName, $download);
    }

    function ImportArray($rs, $fields, $fileName) {
        $excel = new ExcelWriter();
        $data[] = array_values($fields);

        foreach ($rs as $rw) {
            $data[] = array_values($rw);
        }
        $d = date("Ymd");
        $excel->LoadFile($data, "report$d.xls");
        return;
    }

    /*
     * @Params 
     * 		$sql=A valid SQL query.
     * 		$fileName=Name of the file wants to save as.On leaving balnk it takes the table name as file name
     * 		$download=Boolean value whether you wants to save on disk or you want to force to download the file.
     */

    function ImportData($sql, $fileName = "har_excel.xls", $download = false) {
        $excel = new ExcelWriter();
        $data = array();

        $r = $this->db->query($sql);
        if ($this->db->num_rows == 0) {
            $this->error = "No data found in the table";
            return false;
        }
        $r = $this->db->queryToArray($sql, true);
        $f = $r[0];

        $data[] = array_keys($f);

        foreach ($r as $rw) {
            $data[] = array_values($rw);
        }
        $d = date("Ymd");
        $excel->LoadFile($data, "report$d.xls");
        return;
    }

    function download_file($filename, $isDel = false) {
        $file = basename($filename);

        if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
            $file = preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1);
        }

        // make sure the file exists before sending headers
        if (!$fdl = @fopen($filename, 'r')) {
            die("<br>Cannot Open File!<br>");
        } else {
            header("Content-type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=\"$filename\"");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
            header("Pragma: public");
            fpassthru($fdl);
        }
        @unlink($filename);
    }

}

?>